<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :width="width"
    :visible="visible"
    @close="handleCancel"
    destroyOnClose
  >
    <a-spin :spinning="confirmLoading">
      <j-form-container>
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <!--<a-form-model-item label="上一级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="progressId">
                <a-tree-select
                  v-model="progress"
                  :label-in-value="true"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeData"
                  placeholder="请选择"
                  @change="changeConstruction"
                  tree-default-expand-all
                >
                </a-tree-select>
              </a-form-model-item>-->
              <a-form-model-item label="任务名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemName">
                <a-input v-model="model.itemName" placeholder="请输入任务名称"></a-input>
              </a-form-model-item>
              <a-form-model-item label="任务阶段" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="itemStage">
                <j-dict-select-tag
                  placeholder="请选择任务阶段"
                  dictCode="gh_customer_info_decoration_stage"
                  v-model="model.itemStage"
                />
              </a-form-model-item>
              <a-form-model-item label="开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startWorkTime">
                <j-date placeholder="请选择开工时间"  v-model="model.startWorkTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
              </a-form-model-item>

              <a-form-model-item label="工期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workTimeLimit">
                <a-input-number v-model="model.workTimeLimit" placeholder="请输入工期" style="width: 100%" />
              </a-form-model-item>

              <a-form-model-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="personInCharge">
                <j-dict-select-tag
                  placeholder="请选择负责人"
                  dictCode="construction_mould_item_person_in_charge"
                  v-model="model.personInCharge"
                />
              </a-form-model-item>
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
    </a-spin>
    <a-row :gutter="24">
      <a-col :span="24">
        <a-button type="primary" style="width: 100%" @click="handleOk">保存</a-button>
      </a-col>
    </a-row>
  </a-drawer>


  <!--<j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">



  </j-modal>-->
</template>

<script>

  import {getAction, httpAction,postAction} from "../../../../../api/manage";

  export default {
    name: "gantetuModal",
    data() {
      return {
        progress: {
        },
        title: '',
        width: '100%',
        visible: false,
        disableSubmit: false,
        currentItemId: 0,       //当前操作的任务id
        currentMouldId: 0,    //当前模板id
        treeData: [],
        confirmLoading: false,
        currentParentId: 0,
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        model:{},
        validatorRules: {
          itemName: [{ required: true, message: '任务名称不能为空' }],
          startWorkTime: [{ required: true, message: '开工时间不能为空' }],
          workTimeLimit: [
            { required: true, message: '工期不能为空' },
            {"pattern": /^[0-9]*$/,"message": "只能输入正整数"},
            /*{"pattern": /^\d+(?=\.{0,1}\d+$|$)/,"message": "只能输入正数"}*/
          ],
          personInCharge: [{ required: true, message: '负责人不能为空' }],
        },
        url: {
          add: "/ghConstructionMouldItem/add",
          treeDataList: "/ghConstructionMouldItem/treeList",
          edit: "/ghConstructionMouldItem/edit"
        }
      }
    },
    methods: {
      changeConstruction(val){
        this.currentParentId = val.value;
      },
      initTreeData(mouldId) {
        getAction(this.url.treeDataList, {mouldId: mouldId}).then((res) => {
          if (res.success) {
            this.treeData = res.result.records || res.result;
          } else {
            this.$message.warning(res.message)
          }
        })
      },
      add(currentMouldId) {
        this.visible = true
        //this.currentItemId = currentItemId;
        this.currentMouldId = currentMouldId;
        this.initTreeData(currentMouldId);
      },
      edit(record) {
        this.visible = true
        this.model = record;
        this.currentMouldId = record.constructionMouldId;
        this.initTreeData(record.constructionMouldId);
        if (record.parentId == 0){
          this.progress.value = record.id;
        }else {
          this.progress.value = record.parentId;
        }
        this.currentParentId = record.parentId;
      },
      close() {
        this.$emit('close');
        this.visible = false;
        this.model = {};
        this.progress = {};
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        //that.confirmLoading = true;
        /*if (that.progress == null || that.progress == ""){
          that.$message.error("请选择上级目录")
          return;
        }*/
        this.$refs.form.validate(valid => {
          if (valid) {
            that.model.constructionMouldId = that.currentMouldId;
            that.model.parentId = that.currentParentId;

            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += that.url.add;
              method = 'post';
            } else {
              httpurl += that.url.edit;
              method = 'put';
            }
            httpAction(httpurl, that.model, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.close();
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.visible = false;
              that.model = {};
              that.progress = {};
            })
          }
        })
      },
      submitCallback() {
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel() {
        this.close()
        this.model = {};
        this.progress = {};
      }
    }
  }
</script>

<style scoped>
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
</style>